package com.itmiaojie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itmiaojie.mapper.MenuMapper;
import com.itmiaojie.pojo.Menu;
import com.itmiaojie.service.MenuService;
import com.itmiaojie.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;


/**
 *
 */
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService {

    @Override
    public List<String> selectUserPassionsById(Long userId) {
        if (userId == 1L){ //如果为1则为管理员返回所有的权限
            LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Menu::getMenuType,"C","M");
            queryWrapper.eq(Menu::getStatus,"0");
            List<Menu> menuList = list(queryWrapper);
            List<String> perms = menuList
                    .stream()
                    .map(Menu::getPerms)
                    .collect(Collectors.toList());
            return perms;
        }
        //否则返回所具有的权限
        return getBaseMapper().selectPermsByUserId(userId);

    }

    @Override
    public List<Menu> selectRouterMenuTreeByUserId(Long userId) {
        MenuMapper menuMapper = getBaseMapper();
        List<Menu> menus = null;
        if (SecurityUtils.isAdmin()){//如果登陆的是admin
            menus =  menuMapper.selectAllRouterMenu();
        }else {
            //否则  获取当前用户所具有的Menu
            menus = menuMapper.selectRouterMenuTreeByUserId(userId);
        }
       /* List<Menu> menuTree= builderMenuTree(menus,0L);
        log.info("Tree = " + menuTree);*/

        Map<Long,Menu> map = new HashMap<>();
        for (Menu menu : menus) {
            map.put(menu.getId(),menu);
        }
        List<Menu> top = new ArrayList<>();
        for (int i = 0; i < menus.size(); i++) {
            Menu menu = menus.get(i);
            Menu parent = map.get(menu.getParentId());
            if (parent != null){
               setChildrenMenu(parent,menu);
            }else {
                top.add(menu);
            }
        }

        return top;
    }

    private List<Menu> builderMenuTree(List<Menu> menus, Long parentId) {
        List<Menu> menuTree = menus.stream()
                .filter(menu -> menu.getParentId().equals(parentId))
                .map(menu -> {
                    menu.setChildren(getChildren(menu,menus));
                    return menu;
                })
                .collect(Collectors.toList());
        return menuTree;
    }

    private List<Menu> getChildren(Menu menu, List<Menu> menus) {
        List<Menu> children = menus.stream()
                .filter(m -> m.getParentId().equals(menu.getId()))
                .map(m -> {
                    m.setChildren(getChildren(m,menus));
                    return m;
                })
                .collect(Collectors.toList());
        return children;
    }

    private void setChildrenMenu(Menu parent, Menu menu) {
        List<Menu> children = parent.getChildren();
        if (children == null){
            children = new ArrayList<>();
        }
        children.add(menu);
        parent.setChildren(children);
        log.info("parent={},children = {}",parent,children);

    }
}




